﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace ShareHelper
{
    /// <summary>
    /// 窗口配置 保存读取
    /// </summary>
    public static class Configuration
    {
        /// <summary>
        /// 配置保存路径
        /// </summary>
        public static string Personal;

        static Configuration()
        {
            //配置保存路径 我的电脑 文档路径
            Personal = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        }

        /// <summary>
        /// 保存窗口配置
        /// </summary>
        /// <param name="window">窗口</param>
        public static void Save(this Form window)
        {
            Dictionary<string, string> Allocation = new Dictionary<string, string>();
            GetConfiguration(window, Allocation);
            string path = Path.Combine(Personal, window.Text) + ".txt";
            string contents = JsonConvert.SerializeObject(Allocation, Formatting.None);
            File.WriteAllText(path, contents);
        }

        /// <summary>
        /// 读取窗口配置
        /// </summary>
        /// <param name="window">窗口</param>
        /// <param name="data">保存配置变量</param>
        private static void GetConfiguration(Control window, Dictionary<string, string> data)
        {
            foreach (Control item in window.Controls)
            {
                //不保存只读的文本控件 日志输出的吧
                if (item is TextBox textBox && textBox.ReadOnly)
                    continue;
                else if (item is DateTimePicker || item is TextBox)
                    data[item.Name] = item.Text;
                else if (item is RadioButton radioButton)
                    data[radioButton.Name] = radioButton.Checked ? "1" : "0";
                else if (item is CheckBox checkBox)
                    data[checkBox.Name] = checkBox.Checked ? "1" : "0";
                else if (item is GroupBox groupBox)
                    GetConfiguration(groupBox, data);
            }
        }

        /// <summary>
        /// /读取窗口配置
        /// </summary>
        /// <param name="window">窗口</param>
        public static void Read(this Form window)
        {
            string path = Path.Combine(Personal, window.Text) + ".txt";
            if (File.Exists(path))
            {
                string contents = File.ReadAllText(path);
                if (contents.Length > 0)
                {
                    Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(contents);
                    if (data?.Count > 0)
                    {
                        SetConfiguration(window, data);
                    }
                }
            }
        }

        /// <summary>
        /// 设置窗口配置
        /// </summary>
        /// <param name="window">窗口组建</param>
        /// <param name="data">记录配置</param>
        private static void SetConfiguration(Control window, Dictionary<string, string> data)
        {
            foreach (Control item in window.Controls)
            {
                if (item is GroupBox)
                {
                    SetConfiguration(item, data);
                }
                else if (data.TryGetValue(item.Name, out string value))
                {
                    if (item is TextBox textBox && textBox.ReadOnly)
                        continue;
                    else if (item is DateTimePicker || item is TextBox)
                        item.Text = value;
                    else if (item is RadioButton radioButton)
                        radioButton.Checked = "1" == value;
                    else if (item is CheckBox checkBox)
                        checkBox.Checked = "1" == value;
                }
            }
        }
    }
}